{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas\n",
    "import matplotlib.pyplot as plt\n",
    "from PIL import Image\n",
    "import os\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "classes = {\n",
    "            0 : 'smoke',\n",
    "            1 : 'naked',\n",
    "            2 : 'mouse',\n",
    "            3 : 'cat',\n",
    "            4 : 'dog',\n",
    "            5 : 'normal'\n",
    "        }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def convert_bin_to_label(x:int):\n",
    "    bin_value = bin(x)[2:]\n",
    "    label = []\n",
    "    for i in range(len(bin_value)):\n",
    "        if bin_value[i] == '1':\n",
    "            label.append(classes[i])\n",
    "    if not label:\n",
    "        return 'normal'\n",
    "    return ','.join(label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'smoke,naked,mouse'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "convert_bin_to_label(7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "pred = pandas.read_csv('test.csv')\n",
    "file_dir = '/root/code/tianchi/data/test/picture'\n",
    "i = 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  0%|          | 0/5 [00:00<?, ?it/s]"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 5/5 [00:00<00:00, 29.19it/s]\n"
     ]
    }
   ],
   "source": [
    "images = [cv2.imread(os.path.join(file_dir, filename)) for filename in tqdm(pred['filename'][:5])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import shutil\n",
    "shutil.rmtree('out')\n",
    "os.makedirs('out')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 47,  50,  55],\n",
       "        [ 47,  50,  55],\n",
       "        [ 47,  51,  56],\n",
       "        ...,\n",
       "        [ 15,  24,  33],\n",
       "        [ 14,  23,  32],\n",
       "        [ 14,  23,  32]],\n",
       "\n",
       "       [[ 47,  50,  55],\n",
       "        [ 47,  50,  55],\n",
       "        [ 47,  51,  56],\n",
       "        ...,\n",
       "        [ 16,  25,  34],\n",
       "        [ 14,  23,  32],\n",
       "        [ 14,  23,  32]],\n",
       "\n",
       "       [[ 47,  50,  55],\n",
       "        [ 47,  50,  55],\n",
       "        [ 47,  51,  56],\n",
       "        ...,\n",
       "        [ 16,  25,  34],\n",
       "        [ 14,  23,  32],\n",
       "        [ 14,  23,  32]],\n",
       "\n",
       "       ...,\n",
       "\n",
       "       [[ 63,  84, 105],\n",
       "        [ 63,  84, 105],\n",
       "        [ 62,  83, 104],\n",
       "        ...,\n",
       "        [ 83, 110, 130],\n",
       "        [ 86, 113, 133],\n",
       "        [ 87, 114, 134]],\n",
       "\n",
       "       [[ 59,  83, 103],\n",
       "        [ 59,  83, 103],\n",
       "        [ 59,  83, 103],\n",
       "        ...,\n",
       "        [ 72,  95, 117],\n",
       "        [ 74,  97, 119],\n",
       "        [ 75,  98, 120]],\n",
       "\n",
       "       [[ 54,  78,  98],\n",
       "        [ 54,  78,  98],\n",
       "        [ 54,  78,  98],\n",
       "        ...,\n",
       "        [ 59,  80, 102],\n",
       "        [ 60,  81, 103],\n",
       "        [ 61,  82, 104]]], dtype=uint8)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "images[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31mThe Kernel crashed while executing code in the the current cell or a previous cell. Please review the code in the cell(s) to identify a possible cause of the failure. Click <a href='https://aka.ms/vscodeJupyterKernelCrash'>here</a> for more info. View Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
     ]
    }
   ],
   "source": [
    "import cv2\n",
    "cv2.namedWindow(winname='win')\n",
    "cv2.imshow('win',images[0])\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "ename": "error",
     "evalue": "OpenCV(4.8.1) :-1: error: (-5:Bad argument) in function 'imshow'\n> Overload resolution failed:\n>  - imshow() missing required argument 'mat' (pos 2)\n>  - imshow() missing required argument 'mat' (pos 2)\n>  - imshow() missing required argument 'mat' (pos 2)\n",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31merror\u001b[0m                                     Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[18], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mcv2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mimshow\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m/root/code/tianchi/out/ele_caf2c503678dcd27adb8f84accef036e.jpg\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "\u001b[0;31merror\u001b[0m: OpenCV(4.8.1) :-1: error: (-5:Bad argument) in function 'imshow'\n> Overload resolution failed:\n>  - imshow() missing required argument 'mat' (pos 2)\n>  - imshow() missing required argument 'mat' (pos 2)\n>  - imshow() missing required argument 'mat' (pos 2)\n"
     ]
    }
   ],
   "source": [
    "import cv2\n",
    "import os\n",
    "\n",
    "# Assuming you have already loaded 'images', 'pred', and 'convert_bin_to_label' is defined\n",
    "# ...\n",
    "\n",
    "# Iterate over the loaded images and corresponding predictions\n",
    "for img, row in zip(images, pred.itertuples(index=False)):\n",
    "    filename = row.filename\n",
    "    result = row.result\n",
    "\n",
    "    # Display the image with a title\n",
    "    cv2.imshow('Image', img)\n",
    "    \n",
    "    # Set the title using the platform-specific method\n",
    "    if os.name == 'posix':  # Unix/Linux/Mac\n",
    "        cv2.setWindowTitle('Image', f'result: {convert_bin_to_label(int(result))}')\n",
    "    elif os.name == 'nt':  # Windows\n",
    "        cv2.setWindowTitle('Image', f'result: {convert_bin_to_label(int(result))}')\n",
    "\n",
    "    cv2.waitKey(0)  # Wait for a key press before moving to the next image\n",
    "\n",
    "    # Save the image\n",
    "    output_path = os.path.join('out', filename)\n",
    "    cv2.imwrite(output_path, img)\n",
    "\n",
    "cv2.destroyAllWindows()  # Close the window after all images have been displayed\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "dl4",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
